package com.fulihui.information.service;

import com.fulihui.information.api.CategoryService;
import com.fulihui.information.biz.content.CategoryBizManager;
import com.fulihui.information.core.repository.CategoryRepository;
import com.fulihui.information.dal.dataobj.InfoCategoryExample;
import com.fulihui.information.dto.CategoryDTO;
import com.fulihui.information.request.CategoryCodeRequest;
import com.fulihui.information.request.CategoryQueryRequest;
import com.fulihui.information.request.CategorySaveRequest;
import com.fulihui.information.request.CategoryTransformSequenceRequest;
import org.near.servicesupport.error.Errors;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.TMultiResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static org.near.servicesupport.error.Errors.Commons.REQUEST_PARAMETER_ERROR;
import static org.near.servicesupport.result.ResultBuilder.*;

/**
 * @author Willard.Hu on 2016/12/27 0027.
 */
@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryBizManager categoryBizManager;
    @Autowired
    private CategoryRepository categoryRepository;

    /**
     * @see CategoryService#queryGroup(CategoryQueryRequest)
     */
    @Override
    public TMultiResult<CategoryDTO> queryGroup(CategoryQueryRequest request) {
        ServiceAssert.notBlank(request.getProductCode(), REQUEST_PARAMETER_ERROR);
        return succTMulti(categoryBizManager.queryGroup(request.getProductCode(), request.getParentCode()));
    }

    /**
     * @see CategoryService#queryGroupRT(CategoryQueryRequest)
     */
    @Override
    public TMultiResult<CategoryDTO> queryGroupRT(CategoryQueryRequest request) {
        ServiceAssert.notBlank(request.getProductCode(), REQUEST_PARAMETER_ERROR);
        return succTMulti(categoryBizManager.queryGroupRT(request.getProductCode(), request.getParentCode()));
    }

    /**
     * @see CategoryService#query(CategoryQueryRequest)
     */
    @Override
    public TMultiResult<CategoryDTO> query(CategoryQueryRequest request) {
        ServiceAssert.notBlank(request.getProductCode(), REQUEST_PARAMETER_ERROR);
        InfoCategoryExample example = new InfoCategoryExample();
        example.createCriteria().andProductCodeEqualTo(request.getProductCode());
        example.setOrderByClause("sort_index");
        return succTMulti(categoryRepository.query(example));
    }

    /**
     * @see CategoryService#save(CategorySaveRequest)
     */
    @Override
    public BaseResult save(CategorySaveRequest request) {
        CategoryDTO dto = categoryRepository.queryByCode(request.getCode());
        boolean b = true;
        if (dto == null) { // Insert
            ServiceAssert.notBlank(request.getName(), REQUEST_PARAMETER_ERROR);
            ServiceAssert.notBlank(request.getProductCode(), REQUEST_PARAMETER_ERROR);
            ServiceAssert.notNull(request.getShowAdvert(), REQUEST_PARAMETER_ERROR);
            ServiceAssert.notNull(request.getDisplay(), REQUEST_PARAMETER_ERROR);
            categoryRepository.insert(request);
        } else { // Update
            b = categoryRepository.update(request);
        }
        return b ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    /**
     * @see CategoryService#transformSequence(CategoryTransformSequenceRequest)
     */
    @Override
    public BaseResult transformSequence(CategoryTransformSequenceRequest request) {
        boolean b = categoryRepository.transformSequence(request);
        return b ? succ() : fail(Errors.Commons.SYSTEM_ERROR);
    }

    /**
     * @see CategoryService#querySingle(CategoryCodeRequest)
     */
    @Override
    public TSingleResult<CategoryDTO> querySingle(CategoryCodeRequest request) {
        return succTSingle(categoryRepository.queryByCode(request.getCode()));
    }
}
